package com.ch.clinking.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.ch.clinking.R;
import com.ch.clinking.Utils.NetUtils.DoCallBack;
import com.ch.clinking.Utils.NetUtils.OkHttpUtil;
import com.ch.clinking.activity.EditMerStockActivity;
import com.ch.clinking.bean.MerchandiseDto;
import com.ch.clinking.bean.SameKind;
import com.ch.clinking.bean.StockChanged;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

public class SameKindAdapter extends RecyclerView.Adapter<SameKindAdapter.SameKindViewHolder> {
    public List<SameKind> sameKindList;
    private Context context;

    public SameKindAdapter(Context context, List<SameKind> sameKindList) {
        this.context = context;
        this.sameKindList = sameKindList;
    }

    @NonNull
    @Override
    public SameKindAdapter.SameKindViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_same_kind, parent, false);
        return new SameKindAdapter.SameKindViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SameKindViewHolder holder, int position) {
        SameKind sameKind = sameKindList.get(position);

        holder.sameKindSkc.setText(sameKind.getSkcId());

        holder.deleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                HashMap<String, String> sameKindMap = new HashMap<>();
                sameKindMap.put("skcId", sameKind.getSkcId());

                OkHttpUtil.getInstance().doPostForm("/mer/deleteSameKindBySkcId", new DoCallBack() {
                    @SuppressLint("NotifyDataSetChanged")
                    @RequiresApi(api = Build.VERSION_CODES.O)
                    @Override
                    public void onSuccess(String result) {

                        for (SameKind same : sameKindList) {
                            if (same.getSkcId().equals(sameKind.getSkcId())) {
                                // 将不可变的 List 转换为可变的 ArrayList
                                List<SameKind> mutableList = new ArrayList<>(sameKindList);

                                // 向列表中添加新元素
                                mutableList.remove(same);

                                // 如果需要更新原来的 sameKinds 变量
                                sameKindList = Collections.unmodifiableList(mutableList);  // 如果你希望保持不可变性

                                notifyDataSetChanged();
                            }
                        }


                    }

                    @Override
                    public void onError(Exception e) {
                        Toast.makeText(context, "网络错误,删除失败,请稍后再试", Toast.LENGTH_SHORT).show();
                    }
                }, sameKindMap);
            }
        });

    }

    @Override
    public int getItemCount() {
        return sameKindList.size();
    }

    public static class SameKindViewHolder extends RecyclerView.ViewHolder {
        TextView sameKindSkc;
        Button deleteBtn;

        public SameKindViewHolder(@NonNull View itemView) {
            super(itemView);
            sameKindSkc = itemView.findViewById(R.id.popup_same_kind_skc);
            deleteBtn = itemView.findViewById(R.id.popup_same_kind_skc_delete);
        }
    }
}
